package com.tqz.rc.common.util;

import com.tqz.rc.common.constant.Constants;
import com.tqz.rc.common.constant.DefaultValueConstant;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/8/26 17:14
 */
public class NamingUtils {

    public static String getNamespaceId(final String namespaceId) {
        if (StringUtils.isBlank(namespaceId)) {
            return DefaultValueConstant.DEFAULT_NAMESPACE_ID;
        }
        return namespaceId;
    }

    public static String getGroupedName(String serviceName, String groupName) {
        if (StringUtils.isBlank(serviceName)) {
            throw new IllegalArgumentException("Param 'serviceName' is illegal, serviceName is blank");
        }
        if (StringUtils.isBlank(groupName)) {
            throw new IllegalArgumentException("Param 'groupName' is illegal, groupName is blank");
        }
        final String resultGroupedName = groupName + Constants.SERVICE_INFO_SPLITER + serviceName;
        return resultGroupedName.intern();
    }


    public static String getGroupName(final String serviceNameWithGroup) {
        if (StringUtils.isBlank(serviceNameWithGroup)) {
            return StringUtils.EMPTY;
        }
        if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) {
            return DefaultValueConstant.DEFAULT_GROUP;
        }
        return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[0];
    }
}
